using System.Net.Sockets;

namespace Fusion.Net.TCP
{
    public class TCPServerSocket : TCPSocket
    {

        public TCPServerSocket(TCPNet tcp, AddressFamily addressFamily) : base(tcp, addressFamily)
        {
            tcpServer = (TCPServer)tcp;
            socket.Blocking = false;
        }

        protected override void Invoke()
        {
            Socket clientSocket = Socket.Accept();
            TCPClientSocket tcpClientSocket = new TCPClientSocket(tcpServer, clientSocket);
            tcpClientSocket.TCPServer = tcpServer;
            tcpClientSocket.Bind(clientSocket.RemoteEndPoint);
            tcpClientSocket.Start();
            tcpServer.AddClient(tcpClientSocket);
            netBase.DataHandler.Enqueue(tcpClientSocket, new NetDataHandler.DataWrap(null, NetDataHandler.DataType.Connected));
        }

        protected override void PreOpen()
        {
            socket.Bind(ip);
            socket.Listen(10);
        }
    }

}

